let router = require('../router/Router');
let ipUtil = require('../util/ipUtil');

let payService = require('../service/PayService');

const { BusiError, SysError } = require('../exception/exception');

/**
 * 充值接口
 */
//router.post('/auth/pay/recharge', (req, resp) => {
router.post('/player/pay/recharge', (req, resp) => {
    let uid = req.uid;
    if(!uid){
        uid = '12345';
    }
    let ip = ipUtil.getIp(req, resp);
    let amount = req.body.amount;
    if (!amount || amount <= 0){
        resp.send({code: 500, msg: "充值金额不能小于0"});
        return;
    }

    let payChannel = req.body.payChannel;
    if (!payChannel){
        resp.send({code: 500, msg: "渠道不能为空"});
        return;
    }

    let category = req.body.category;
    if (!category){
        resp.send({code: 500, msg: "category不能为空"});
        return;
    }

    if(category === 'C2' && amount < 100 * 100){
        resp.send({code: 500, msg: "充值金额必须大于等于100"});
        return;
    }

    //test
    // let uid = '55555';
    // let amount = 500;
    // let category = 'C3';
    // let payChannel = 'JuHe';

    payService.getService(payChannel).pay(uid, amount, category).then(res => {
        if (payChannel == "JuHe"){
            resp.redirect(res.payinfo);
            return;
        }
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.log("err:", err);
        let msg = "系统异常, 请联系客服";
        if (err instanceof BusiError){
            msg = err.message;
        }
        resp.send({code: 500, msg: msg});
    });
});

/**
 * 充值查询接口
 */
router.post('/auth/pay/query', (req, resp) => {
    let uid = req.uid;
    let outTradeNo = req.body.outTradeNo;
    if (!outTradeNo || outTradeNo === ""){
        resp.send({code: 500, msg: "订单号不能为空"});
        return;
    }

    let payChannel = req.body.payChannel;
    if (!payChannel){
        resp.send({code: 500, msg: "渠道不能为空"});
        return;
    }

    payService.getService(payChannel).payQuery(outTradeNo).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.log("err:", err);
        let msg = "系统异常, 请联系客服";
        if (err instanceof BusiError){
            msg = err.message;
        }
        resp.send({code: 500, msg: msg});
    });
});

/**
 * 聚合接口
 */
router.post('/pay/qft/notify/juhe', (req, resp) => {
    console.log("req.body:", req.body);
    payService.getService("JuHe").payNotify(req.body).then(res => {
        resp.send("SUCCESS");
    }).catch(err => {
        console.log("err:", err);
        resp.send("FAIL");
    });
});


/**
 * 中信全付通银联wap接口
 */
router.post('/pay/qft/notify/union', (req, resp) => {
    console.log("req.body:", req.body);
    payService.getService("J001").payNotify(req.body).then(res => {
        resp.send("success");
    }).catch(err => {
        console.log("err:", err);
        resp.send("fail");
    });
});

/**
 * 中信全付通银联二维码接口
 */
router.post('/pay/qft/notify/union/native', (req, resp) => {
    console.log("req.body:", req.body);
    payService.getService("J004").payNotify(req.body).then(res => {
        resp.send("success");
    }).catch(err => {
        console.log("err:", err);
        resp.send("fail");
    });
});

/**
 * 中信全付通支付宝wap接口
 */
router.post('/pay/qft/notify/alipay', (req, resp) => {
    console.log("req.body:", req.body);
    payService.getService("J003").payNotify(req.body).then(res => {
        resp.send("success");
    }).catch(err => {
        console.log("err:", err);
        resp.send("fail");
    });
});

/**
 * 中信全付通微信二维码接口
 */
router.post('/pay/qft/notify/wechat', (req, resp) => {
    console.log("req.body:", req.body);
    payService.getService("J002").payNotify(req.body).then(res => {
        resp.send("success");
    }).catch(err => {
        console.log("err:", err);
        resp.send("fail");
    });
});

